//
//  XMChatCustomCell.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/5.
//

#import "XMChatCustomCell.h"

@interface XMChatCustomCell ()

@property (nonatomic, strong) UIView *contentBgView;

@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UIImageView *converImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) UIButton *getBtn;

@property (nonatomic, strong) NSDictionary *meetDic;

@end

@implementation XMChatCustomCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xF7F7F7);
    [self.contentView addSubview:self.contentBgView];
    [_contentBgView addSubview:self.topLabel];
    [_contentBgView addSubview:self.converImgView];
    [_contentBgView addSubview:self.titleLabel];
    [_contentBgView addSubview:self.priceLabel];
    [_contentBgView addSubview:self.addressLabel];
    [_contentBgView addSubview:self.timeLabel];
    
    UIButton *getBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(_contentBgView.width - KScaleWidth(17.0) - KScaleWidth(70.0), _contentBgView.height - KScaleWidth(10.0) - KScaleWidth(34.0), KScaleWidth(70.0), KScaleWidth(34.0)) title:@"领取" titleColor:kColorHEX(0x000000)];
    getBtn.backgroundColor = kColorHEX(0xFFD800);
    [getBtn setCornerRadius:getBtn.height/2];
    [getBtn addTarget:self action:@selector(getAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentBgView addSubview:getBtn];
    
    UIButton *contentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    contentBtn.frame = _contentBgView.bounds;
    [contentBtn addTarget:self action:@selector(getAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentBgView addSubview:contentBtn];
}

- (void)setContentWithMessage:(V2TIMMessage *)message
                 senderAvatar:(NSString *)senderAvatarImg
               receiverAvatar:(NSString *)receiverAvatarImg {
    self.isSelf = message.isSelf;
    if (message.elemType == V2TIM_ELEM_TYPE_CUSTOM) {
        if (message.isSelf) {
            self.avatarImg.x = kScreenWidth - KScaleWidth(10.0) - self.avatarImg.width;
            [self.avatarImg sd_setImageWithURL:[NSURL URLWithString:senderAvatarImg] placeholderImage:ImageNamed(@"mine_avatar_icon")];
            _contentBgView.x = self.avatarImg.x - KScaleWidth(10.0) - _contentBgView.width;
        }
        else {
            self.avatarImg.x = KScaleWidth(10.0);
            [self.avatarImg sd_setImageWithURL:[NSURL URLWithString:receiverAvatarImg] placeholderImage:ImageNamed(@"mine_avatar_icon")];
            
            _contentBgView.x = self.avatarImg.maxX + KScaleWidth(10.0);
        }
    }
    NSString *dataStr = [[NSString alloc] initWithData:message.customElem.data encoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[dataStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&error];
    _meetDic = dic;
    if (error) {
        NSLog(@"%@", error);
    }
    [_converImgView sd_setImageWithURL:[NSURL URLWithString:[dic safeStringForKey:@"coverImg"]]];
    _titleLabel.text = [dic safeStringForKey:@"title"];
    _addressLabel.text = [NSString stringWithFormat:@"地址：%@", [dic safeStringForKey:@"address"]];
    _priceLabel.text = [NSString stringWithFormat:@"¥%@", [dic safeStringForKey:@"price"]];
    NSString *timeStr = [[NSDate dateWithTimeIntervalSince1970:[dic safeIntegerForKey:@"time"]/1000] dateStringWithFormat:@"yyyy-MM-dd HH:mm"];
    _timeLabel.text = [NSString stringWithFormat:@"时间：%@", timeStr];
    
    NSString *type = @"";
    if ([dic safeIntForKey:@"orderType"] == 1) {
        type = @"转卖";
    }
    else {
        type = @"转赠";
    }
    self.topLabel.text = [NSString stringWithFormat:@"来自%@的%@", [dic safeStringForKey:@"nickName"], type];
//     [parameters safe_setObject:@"" forKey:@"giveInfo"];
//     [parameters safe_setObject:self.orderDetailModel.price forKey:@"price"];
//     [parameters safe_setObject:self.orderDetailModel.address forKey:@"address"];
//     [parameters safe_setObject:self.orderDetailModel.serviceTime forKey:@"time"];
//     [parameters safe_setObject:self.orderDetailModel.coverImageUrl forKey:@"coverImg"];
//     [parameters safe_setObject:self.orderDetailModel.title forKey:@"title"];
//     [parameters safe_setObject:@"2" forKey:@"orderType"];
}

//接受赠送/转卖
- (void)getAction:(UIButton *)button {
//    if (!self.isSelf) {
        self.getGiveBlock ? self.getGiveBlock(self.meetDic) : nil;
//    }
}

#pragma mark - Init

- (UILabel *)topLabel {
    if (!_topLabel) {
        _topLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), KScaleWidth(16.0), self.contentBgView.width - KScaleWidth(10.0), KScaleWidth(24.0)) font:KBoldFontSize(17.0) textColor:kColorHEX(0x333333)];
    }
    return _topLabel;
}

- (UIView *)contentBgView {
    if (!_contentBgView) {
        _contentBgView = [[UIView alloc] initWithFrame:CGRectMake(self.avatarImg.maxX + KScaleWidth(10.0), self.avatarImg.y, KScaleWidth(224.0), KScaleWidth(235.0))];
        _contentBgView.backgroundColor = kColorHEX(0xffffff);
        [_contentBgView setCornerRadius:KScaleWidth(18.0)];
    }
    return _contentBgView;
}

- (UIImageView *)converImgView {
    if (!_converImgView) {
        _converImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(13.0), KScaleWidth(54.0), KScaleWidth(60.0), KScaleWidth(60.0))];
        _converImgView.backgroundColor = kColorHEX(0xD8D8D8);
        [_converImgView setCornerRadius:KScaleWidth(12.0)];
    }
    return _converImgView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.converImgView.maxX + KScaleWidth(5.0), _converImgView.y, self.contentBgView.width - _converImgView.maxX - KScaleWidth(5.0)*2, KScaleWidth(25.0)) font:KFontSize(15.0) textColor:kColorHEX(0x333333)];
    }
    return _titleLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.x, _titleLabel.maxY + KScaleWidth(12.0), _titleLabel.width, KScaleWidth(21.0)) font:KFontSize(15.0) textColor:kColorHEX(0x333333)];
    }
    return _priceLabel;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(13.0), self.converImgView.maxY + KScaleWidth(10.0), self.contentBgView.width - KScaleWidth(13.0)*2, KScaleWidth(21.0)) font:KFontSize(15.0) textColor:kColorHEX(0x4B4B4B)];
    }
    return _addressLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.addressLabel.x, _addressLabel.maxY + KScaleWidth(5.0), _addressLabel.width, _addressLabel.height) font:KFontSize(15.0) textColor:kColorHEX(0x4B4B4B)];
    }
    return _timeLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
